import axios from 'axios';
import { Base64 } from 'js-base64';

// 删除停产信息
export async function deleteStopProduct(id){
    let sqlStatement = "DELETE FROM t_elo_info WHERE t_id="+id;
    const sql = Base64.encode(sqlStatement);
    // 发送POST请求到后端API，并传递参数
    const response = await axios.post(
        "http://8.134.79.23:34343/sql_actuator/execute",
        {
            "env": 'internet-ms-pe',
            "sql": sql,
            "executeType": 1,
        }
    );
    return response.status;
}

// 查询停产信息
export async function searchStopProduct(searchStartDate,searchEndDate){
    // console.log(searchStartDate,searchEndDate)
    let sqlStatement = "SELECT t.t_id,t.t_start_date,t.t_end_date,t.t_reason,t.t_create,ROUND(DATEDIFF(SECOND, t.t_start_date,t.t_end_date) / 3600.0, 2) as t_timeLength from t_elo_info t " +
                    "     where ((t.t_start_date BETWEEN '"+searchStartDate+"' and '"+searchEndDate+"') or (t.t_end_date BETWEEN '"+searchStartDate+"' and '"+searchEndDate+"')) order by t.t_start_date";
    const sql = Base64.encode(sqlStatement);
    // 发送POST请求到后端API，并传递参数
    const response = await axios.post(
        "http://8.134.79.23:34343/sql_actuator/execute",
        {
            "env": 'internet-ms-pe',
            "sql": sql,
        }
    );
    let responseData = response.data.data;
    return responseData;
}

//新增停产时间段
export async function addStopProduct(addFormData){
    // console.log(addFormData.startDate,addFormData.endDate)
    let sqlStatement = "INSERT INTO t_elo_info (t_start_date, t_end_date, t_reason) " +
        "VALUES( '"+addFormData.startDate+"', '"+addFormData.endDate+"', '"+addFormData.reason+"')";
    // console.log(sqlStatement)
    const sql = Base64.encode(sqlStatement);
    // 发送POST请求到后端API，并传递参数
    const response = await axios.post(
        "http://8.134.79.23:34343/sql_actuator/execute",
        {
            "env": 'internet-ms-pe',
            "sql": sql,
            "executeType": 1,
        }
    );
    return response.status;
}

// 超时监控数据看板
export async function getPatternMaking(searchStartDate,searchEndDate,orderNo,clothLineName,pendType,timeoutType,componentType,productCode,productName,colorName) {
    try {
        let sqlStatement = "WITH time_ranges AS ( " +
            "SELECT DATE_SUB(NOW(),INTERVAL 30 DAY) AS thirty_days_ago, " +
            "   DATE_SUB(NOW(),INTERVAL 70 DAY) AS seventy_days_ago, " +
            "   DATE_SUB(NOW(),INTERVAL 100 DAY) AS hundred_days_ago " +
            "), " +
            "base_info AS ( " +
            "  SELECT order_no AS 打板单号, " +
            "     sample_doc_no AS 打样单号, " +
            "     a.dyelot_code AS 缸号, " +
            "     COALESCE (b.交期,c.交期) AS 交期, " +
            "     CASE WHEN order_state='1' THEN '未接收' WHEN order_state='2' THEN '已接收' ELSE '' END AS 是否接收, " +
            "     CASE WHEN type='1' THEN '头缸复办' WHEN type='2' THEN '新纱牌复办' WHEN type='3' THEN '打板' WHEN type='4' THEN '染部复办' ELSE '' END AS 打板类型, " +
            "     a.cloth_line_name AS 客户, " +
            "     b.salesman AS 销售, " +
            "     cloth_line_customer AS 布行客户, " +
            "     a.product_code AS 产编, " +
            "     a.product_name AS 布种, " +
            "     dyeing_color_code AS 色号, " +
            "     a.color_name AS 颜色, " +
            "     a.examination_date AS 化验室接收时间, " +
            "     TIMESTAMPDIFF(HOUR,a.examination_date,NOW()) AS 审核时长, " +
            "     null AS 停产时长, " +
            "     a.dyeing_type_name as 染色分类  " +
            "  FROM pms_dyelot_pattern_making a  " +
            "   LEFT JOIN (SELECT NO,salesman,cloth_line_customer,finish_time AS 交期 FROM bms_sample_doc  " +
            "         WHERE created_at> (SELECT thirty_days_ago FROM time_ranges) AND is_deleted='0' " +
            "   ) b ON b.NO=a.sample_doc_no  " +
            "   LEFT JOIN (SELECT a.dyelot_code AS 缸号,c.completed_at AS 交期 FROM pms_dyelot_info a  " +
            "         LEFT JOIN pms_process_order c ON c.id=a.doc_id  " +
            "         WHERE a.created_at> (SELECT seventy_days_ago FROM time_ranges)  " +
            "         AND c.created_at> (SELECT seventy_days_ago FROM time_ranges) " +
            "   ) c ON c.缸号=a.dyelot_code  " +
            "  WHERE a.type IN ('3','1','2','4') AND a.state='1' " +
            "   AND a.created_at> (SELECT thirty_days_ago FROM time_ranges)  " +
            "   AND a.end_date IS NULL AND a.publish_date IS NULL  " +
            "   AND a.examination_date IS NOT NULL  " +
            "  ORDER BY a.examination_date " +
            "), " +
            "component_info AS ( " +
            "SELECT e.* FROM (SELECT d.产编,d.成份分类,d.缸数,ROW_NUMBER() OVER (PARTITION BY d.产编 ORDER BY d.缸数 DESC) AS 编号  " +
            "        FROM (SELECT c.product_code AS 产编,c.成份分类,COUNT(c.dyelot_code) AS 缸数  " +
            "           FROM (SELECT DISTINCT b.dyelot_code, " +
            "                 b.product_code, " +
            "                 (CASE WHEN b.醋酸 IS NOT NULL THEN '醋酸'  " +
            "                   WHEN b.棉>=90 THEN '棉' WHEN b.棉>=80 AND b.安纶 IS NOT NULL THEN '棉'  " +
            "                   WHEN b.棉>=60 AND b.聚酯纤维 IS NOT NULL THEN 'CVC'  " +
            "                   WHEN b.棉< 60 AND b.聚酯纤维 IS NOT NULL THEN 'TC'  " +
            "                   WHEN b.棉< 60 AND b.涤 IS NOT NULL THEN 'TC'  " +
            "                   WHEN b.棉+b.安纶>=80 THEN '棉'  " +
            "                   WHEN b.莱赛尔> 80 THEN '棉'  " +
            "                   WHEN b.有机棉> 80 THEN '有机棉'  " +
            "                   WHEN b.聚酯纤维> 90 THEN 'T'  " +
            "                   WHEN b.涤+b.聚酯纤维> 90 THEN 'T'  " +
            "                   WHEN b.人棉> 80 THEN 'R'  " +
            "                   WHEN b.人棉< 80 AND b.聚酯纤维 IS NOT NULL THEN 'TR'  " +
            "                   WHEN b.人棉< 80 AND (b.聚酯纤维 IS NOT NULL OR b.涤 IS NOT NULL) THEN 'TR'  " +
            "                   WHEN b.腈纶 IS NOT NULL AND b.棉 IS NOT NULL THEN 'TC'  " +
            "                   WHEN b.醋酸 IS NOT NULL THEN '醋酸'  " +
            "                 ELSE '其他' END) AS 成份分类  " +
            "              FROM (SELECT a.dyelot_code,a.product_code, " +
            "                    MAX(CASE WHEN component_name='棉' THEN percentage END) AS 棉, " +
            "                    MAX(CASE WHEN component_name='人棉' THEN percentage END) AS 人棉, " +
            "                    MAX(CASE WHEN component_name='涤' THEN percentage END) AS 涤, " +
            "                    MAX(CASE WHEN component_name='粘胶丝' THEN percentage END) AS 粘胶丝, " +
            "                    MAX(CASE WHEN component_name='莱赛尔' THEN percentage END) AS 莱赛尔, " +
            "                    MAX(CASE WHEN component_name='有机棉' THEN percentage END) AS 有机棉, " +
            "                    MAX(CASE WHEN component_name='氨纶' THEN percentage END) AS 氨纶, " +
            "                    MAX(CASE WHEN component_name='聚酯纤维' THEN percentage END) AS 聚酯纤维, " +
            "                    MAX(CASE WHEN component_name='安纶' THEN percentage END) AS 安纶, " +
            "                    MAX(CASE WHEN component_name='再生涤纶' THEN percentage END) AS 再生涤纶, " +
            "                    MAX(CASE WHEN component_name='锦纶' THEN percentage END) AS 锦纶, " +
            "                    MAX(CASE WHEN component_name='粘胶' THEN percentage END) AS 粘胶, " +
            "                    MAX(CASE WHEN component_name='木代尔' THEN percentage END) AS 木代尔, " +
            "                    MAX(CASE WHEN component_name='醋酸' THEN percentage END) AS 醋酸, " +
            "                    MAX(CASE WHEN component_name='绢丝' THEN percentage END) AS 绢丝, " +
            "                    MAX(CASE WHEN component_name='天丝' THEN percentage END) AS 天丝, " +
            "                    MAX(CASE WHEN component_name='腈纶' THEN percentage END) AS 腈纶, " +
            "                    MAX(CASE WHEN component_name='羊毛' THEN percentage END) AS 羊毛, " +
            "                    MAX(CASE WHEN component_name='博拉' THEN percentage END) AS 博拉, " +
            "                    MAX(CASE WHEN component_name='银线' THEN percentage END) AS 银线  " +
            "                 FROM (SELECT DISTINCT dyelot_code,a.product_code,d.component_name,d.percentage  " +
            "                    FROM pms_dyelot_info a  " +
            "                     LEFT JOIN pms_dyelot_distribution_info b ON b.dyelot_id=a.id  " +
            "                     LEFT JOIN sms_pei_detail c ON c.id=b.source_detail_id  " +
            "                     LEFT JOIN bms_yarn_component_info d ON d.yarn_id=c.yarn_id  " +
            "                    WHERE a.created_at> (SELECT hundred_days_ago FROM time_ranges)  " +
            "                    AND b.created_at> (SELECT hundred_days_ago FROM time_ranges)  " +
            "                    AND b.is_deleted='0' AND c.created_at> (SELECT hundred_days_ago FROM time_ranges)  " +
            "                    AND c.is_deleted='0' AND component_name IS NOT NULL  " +
            "                    AND reset_source <> '布行返修' " +
            "                 ) AS a  " +
            "                 GROUP BY a.dyelot_code,a.product_code " +
            "              ) AS b " +
            "           ) AS c GROUP BY c.product_code,c.成份分类  " +
            "           ORDER BY c.product_code,COUNT(c.dyelot_code) DESC " +
            "        ) AS d " +
            "     ) AS e WHERE e.编号=1 " +
            ") " +
            " " +
            "SELECT d.*,CASE WHEN d.超时标准<=d.审核时长 THEN '超时' ELSE '' END AS 是否超时  " +
            "FROM (SELECT DISTINCT a.*, " +
            "      (CASE WHEN a.客户='千城' THEN '醋酸' ELSE c.成份分类 END) AS 成分分类, " +
            "      (case when a.染色分类 like '%单%' then '36' " +
            "         when a.染色分类 like '%双%' then '48' " +
            "       else (CASE WHEN c.成份分类 IN ('CVC','TC','TR') THEN '48'  " +
            "         WHEN c.成份分类 IN ('T','棉','R','有机棉') THEN '36'  " +
            "         WHEN a.客户='千城' THEN '36'  " +
            "         ELSE '48' END)  " +
            "      end) AS 超时标准  " +
            "   FROM base_info a  " +
            "    LEFT JOIN component_info c ON c.产编=a.产编  " +
            "    where 1 = 1 ";

        if (chekParametersEmpty(orderNo)){
            sqlStatement += " and a.打板单号 like '%"+orderNo+"%'";
        }
        if (chekParametersEmpty(clothLineName)){
            sqlStatement += " and a.客户 like '%"+clothLineName+"%'";
        }
        if (chekParametersEmpty(pendType)){
            sqlStatement += " and a.打板类型 like '%"+pendType+"%'";
        }
        if (chekParametersEmpty(componentType)){
            sqlStatement += " and c.成份分类 = '"+componentType+"'";
        }
        if (chekParametersEmpty(productCode)){
            sqlStatement += " and a.产编 like '%"+productCode+"%'";
        }
        if (chekParametersEmpty(productName)){
            sqlStatement += " and a.布种 like '%"+productName+"%'";
        }
        if (chekParametersEmpty(colorName)){
            sqlStatement += " and a.颜色 like '%"+colorName+"%'";
        }

        sqlStatement += "   ORDER BY a.化验室接收时间 " +
            ") AS d";

        if (chekParametersEmpty(timeoutType)){
            if (timeoutType==1){
                sqlStatement += " where d.超时标准 <= d.审核时长 ";
            }else {
                sqlStatement += " where d.超时标准 > d.审核时长 ";
            }
        }
        const sql = Base64.encode(sqlStatement);
        // 发送POST请求到后端API，并传递参数
        const response = await axios.post(
            // "http://8.134.79.23:34343/sql_actuator/execute",
            "http://localhost:34343/sql_actuator/execute",
            {
                "env": 'intranet-erp',
                "sql": sql,
            }
        );
        let responseData = response.data.data;
        if (responseData!=undefined){
            let sqlStatement = "SELECT t.t_id,t.t_start_date,t.t_end_date,t.t_reason,t.t_create,ROUND(DATEDIFF(SECOND, t.t_start_date,t.t_end_date) / 3600.0, 2) as t_timeLength from t_elo_info t " +
                "     where ((t.t_start_date BETWEEN '"+searchStartDate+"' and '"+searchEndDate+"') or (t.t_end_date BETWEEN '"+searchStartDate+"' and '"+searchEndDate+"'))";
            const sql = Base64.encode(sqlStatement);
            // 发送POST请求到后端API，并传递参数
            const response_sp = await axios.post(
                "http://8.134.79.23:34343/sql_actuator/execute",
                {
                    "env": 'internet-ms-pe',
                    "sql": sql,
                }
            );
            let res_sp = response_sp.data.data;
            responseData = checkTime(responseData,res_sp)
        }
        return responseData;
    } catch (error) {
        console.error('Error fetching data:', error);
        throw error;
    }
}

function checkTime(responseData,responseData_sp){
    if (Array.isArray(responseData)) {
        responseData.forEach((record) => {
            const examination_date = record.化验室接收时间;
            let examination_times = record.审核时长;
            const timeout_standard = record.超时标准;
            let sum_timeLength = 0
            if (Array.isArray(responseData_sp)) {
                responseData_sp.forEach((re) => {
                    const startDatesp = re.t_start_date; //停产开始时间
                    const timeLength = re.t_timeLength; //停产时长
                    if (examination_date<startDatesp){
                        sum_timeLength += timeLength
                    }
                });
            }
            if (sum_timeLength>0){
                record.停产时长 = sum_timeLength
                examination_times = examination_times-sum_timeLength
                //判断 是否超时
                if(timeout_standard<=examination_times){
                    record.是否超时 = '超时'
                }else{
                    record.是否超时 = ''
                }
            }
        });
    }
    return responseData
}

//判断参数是否正确
function chekParametersEmpty(...args) {
    for (const arg of args) {
        if (arg === "*" || arg === null || arg === undefined || arg === "") {
            return false; // 如果任何一个参数为空，返回 false
        }
    }
    return true; // 如果所有参数都不为空，返回 true
}

export async function getSupconDatasHDD(day,machinename,batchname,dyeprogname) {
    try {
        console.log(day,machinename,batchname,dyeprogname)
        // 使用模板字符串或参数绑定来构造SQL语句（取决于你的后端API如何处理参数）
        const sqlStatement = "";
        const sql = Base64.encode(sqlStatement);
        // 发送POST请求到后端API，并传递参数
        const response = await axios.post(
            "http://8.134.79.23:34343/sql_actuator/execute",
            {
                "env": 'internet-ms-pe',
                "sql": sql,
            }
        );
        const responseData = response.data.data;
        return responseData;
    } catch (error) {
        console.error('Error fetching data:', error);
        throw error; // 抛出错误以便在组件中处理
    }
}

export function getTest1(url,parameter){
    return axios({
        url: url,
        method: 'get',
        params: parameter
    })
}

export async function getTest2(url,parameter){
    await axios.post(
        url,
        parameter
    )
}

export async function getMachineDetaiPie(day) {
    try {
        if (null == day){
            day = 100;
        }
        const sqlStatement = " SELECT tavg.machinename,tavg.batchname,tavg.dyeprogname,round(sum(tavg.water_loading_rate)/count(1),2) as wlavg_rate from \n" +
            " (SELECT m.machinename,m.batchname,m.dyeprogname,\n" +
            "     (case when m.loading = 0 then 0 else round((m.water_vol4_total_all/m.loading*1000),2) end) as water_loading_rate\n" +
            "  from t_machine_info m \n" +
            "  WHERE m.create_time BETWEEN DATEADD(DAY, -"+day+", GETDATE()) AND GETDATE()\n" +
            "  and m.water_vol4_total_all > 0) tavg\n" +
            "  group by tavg.machinename,tavg.batchname,tavg.dyeprogname";
        const sql = Base64.encode(sqlStatement);
        // 发送POST请求到后端API，并传递参数
        const response = await axios.post(
            "http://8.134.79.23:34343/sql_actuator/execute",
            {
                "env": 'internet-ms-pe',
                "sql": sql,
            }
        );
        const responseData = response.data.data;
        return responseData;
    } catch (error) {
        console.error('Error fetching data:', error);
        throw error;
    }
}

export async function getMachineDetaiLine(day,machinename) {
    try {
        if (null == day){
            day = 100;
        }
        const sqlStatement = "SELECT m.machinename,m.batchname,m.dyeprogname,\n" +
            "     convert(varchar,m.create_time,120) as runtime,\n" +
            "     (case when m.machinestate = '停止' then 0 else 10 end) as machinestate,\n" +
            "     (case when m.machinemode = '单机' then 0 else 20 end) as machinemode,\n" +
            "     75 as standard_line,\n" +
            "     m.water_vol4_total_all,\n" +
            "     m.loading,\n" +
            "     (case when m.loading = 0 then 0 else round((m.water_vol4_total_all/m.loading*1000),2) end) as water_loading_rate\n" +
            "  from t_machine_info m \n" +
            "  WHERE m.create_time BETWEEN DATEADD(DAY, -"+day+", GETDATE()) AND GETDATE()\n" +
            "   and m.machinename = '"+machinename+"'\n" +
            "  order by m.machinename,convert(varchar,m.create_time,120)";
        const sql = Base64.encode(sqlStatement);
        // 发送POST请求到后端API，并传递参数
        const response = await axios.post(
            "http://8.134.79.23:34343/sql_actuator/execute",
            {
                "env": 'internet-ms-pe',
                "sql": sql,
            }
        );
        const responseData = response.data.data;
        return responseData;
    } catch (error) {
        console.error('Error fetching data:', error);
        throw error;
    }
}

export async function getSupconDatasHD(day,machinename) {
    try {
        if (null == day){
            day = 10;
        }
        const sqlStatement = "SELECT m.machinename,m.batchname,m.tubes,CONVERT(varchar(10), m.create_time, 120) as rundate,m.dyeprogname,\n" +
            "             dense_rank() over(partition by m.machinename,CONVERT(varchar(10), m.create_time, 120) order by m.dyeprogname) as t_rank,\n" +
            "             min(CONVERT(varchar, m.create_time, 120)) as starttime,\n" +
            "             max(CONVERT(varchar, m.create_time, 120)) as endtime\n" +
            "             from t_machine_info m \n" +
            "             WHERE m.create_time BETWEEN DATEADD(DAY, -"+day+", GETDATE()) AND GETDATE()\n" +
            "               and m.machinename = '"+machinename+"'\n" +
            "             group by m.machinename,m.batchname,m.tubes,CONVERT(varchar(10), m.create_time, 120),m.dyeprogname  \n" +
            "             order by CONVERT(varchar(10), m.create_time, 120),m.dyeprogname";

        const sql = Base64.encode(sqlStatement);
        // 发送POST请求到后端API，并传递参数
        const response = await axios.post(
            "http://8.134.79.23:34343/sql_actuator/execute",
            {
                "env": 'internet-ms-pe',
                "sql": sql,
            }
        );
        const responseData = response.data.data;
        return responseData;
    } catch (error) {
        console.error('Error fetching data:', error);
        throw error;
    }
}

export async function getSupconDatasH(day) {
    try {
        if (null == day){
            day = 100;
        }
        const sqlStatement = "SELECT t.machinename,t.tubes,count(1) AS count_machinename           \n" +
            "           from (SELECT m.machinename,m.batchname,m.tubes,CONVERT(varchar(10), m.create_time, 120) as rundate,m.dyeprogname,\n" +
            "             dense_rank() over(partition by m.machinename,CONVERT(varchar(10), m.create_time, 120) order by m.dyeprogname) as t_rank,\n" +
            "             min(CONVERT(varchar, m.create_time, 120)) as starttime,\n" +
            "             max(CONVERT(varchar, m.create_time, 120)) as endtime\n" +
            "             from t_machine_info m \n" +
            "             WHERE m.create_time BETWEEN DATEADD(DAY, -"+day+", GETDATE()) AND GETDATE() \n" +
            "             group by m.machinename,m.batchname,m.tubes,CONVERT(varchar(10), m.create_time, 120),m.dyeprogname) t\n" +
            "           group by t.machinename,t.tubes order by t.machinename,t.tubes";
        const sql = Base64.encode(sqlStatement);
        // 发送POST请求到后端API，并传递参数
        const response = await axios.post(
            "http://8.134.79.23:34343/sql_actuator/execute",
            {
                "env": 'internet-ms-pe',
                "sql": sql,
            }
        );
        const responseData = response.data.data;
        return responseData;
    } catch (error) {
        console.error('Error fetching data:', error);
        throw error;
    }
}

export async function expandRowsH(pei_type,cloth_line_name,product_code,product_name,startDate,endDate,outlossrate) {
    try {
        // 使用模板字符串或参数绑定来构造SQL语句（取决于你的后端API如何处理参数）
        const sqlStatement = "SELECT t.product_code,t.product_name,t.degree_of_staining_code, \n" +
            "                        t.degree_of_staining_name,count(1) as detail_count, \n" +
            "                        case when count(1) = 0 then 0 else round(sum(t.loss_rate)/count(1),2) end AS avg_loss_rate, \n" +
            "                        sum(case when t.loss_rate > "+outlossrate+" then 1 else 0 end) AS out_loss_rate,t.t_cloth_line_name as cloth_line_name,t.pei_type, \n" +
            "                        t.label_print_gsm,t.workmanship_pay_project_name,t.ingredients_type,t.remarks,t.reset_source,sum(t.fabric_sum) as fabric_sum \n" +
            "                         FROM (SELECT l.t_dyelot_code,\n" +
            "                          l.t_product_code as product_code,\n" +
            "                          l.t_product_name as product_name,\n" +
            "                          l.t_label_color_name as label_color_name,\n" +
            "                          l.t_fabric_sum as fabric_sum,\n" +
            "                          l.t_fabric_label_weight_sum as fabric_label_weight_sum,\n" +
            "                          l.t_pi_sum as pi_sum,\n" +
            "                          l.t_xima_sum as xima_sum,\n" +
            "                          l.t_kongcha as kongcha,\n" +
            "                          l.t_tube_weight as tube_weight,\n" +
            "                          l.t_loss_rate as loss_rate,\n" +
            "                          l.t_remarks as remarks,\n" +
            "                          l.t_degree_of_staining_code as degree_of_staining_code,\n" +
            "                          l.t_degree_of_staining_name as degree_of_staining_name,\n" +
            "                          FORMAT(l.t_in_date, 'yyyy-MM-dd') as in_date,\n" +
            "                          d.t_cloth_line_name,\n" +
            "                          l.pei_type, \n" +
            "                          l.t_label_print_gsm as label_print_gsm, \n" +
            "                          l.t_workmanship_pay_project_name as workmanship_pay_project_name, \n" +
            "                          l.t_ingredients_type as ingredients_type, \n" +
            "                          (case when l.t_reset_source='' then '/' else l.t_reset_source end) as reset_source \n" +
            "                         from t_io_doc_loss l left join t_dyelot_info d on l.t_dyelot_code = d.t_dyelot_code \n" +
            "                        where l.t_in_date BETWEEN CONVERT(datetime, '"+startDate+" 00:00:00') and CONVERT(datetime, '"+endDate+" 23:59:59')  \n" +
            "                           and l.pei_type like '%"+pei_type+"%' and d.t_cloth_line_name like '%"+cloth_line_name+"%' and l.t_product_name like '%"+product_name+"%' and l.t_product_code like '%"+product_code+"%' ) t  \n" +
            "                 where (t.loss_rate<100 or (t.loss_rate=100 and t.product_code='000#'))  \n" +
            "                 GROUP BY t.product_code,t.product_name,t.degree_of_staining_code,t.degree_of_staining_name,t.t_cloth_line_name,t.pei_type, \n" +
            "                          t.label_print_gsm,t.workmanship_pay_project_name,t.ingredients_type,t.remarks,t.reset_source ";

        const sql = Base64.encode(sqlStatement);
        // 发送POST请求到后端API，并传递参数
        const response = await axios.post(
            "http://8.134.79.23:34343/sql_actuator/execute",
            {
                "env": 'internet-ms-pe',
                "sql": sql,
            }
        );
        const responsedatas = response.data.data;
        return responsedatas;
    } catch (error) {
        console.error('Error fetching data:', error);
        throw error; // 抛出错误以便在组件中处理
    }
}

export async function expandRowsExportD(pei_type,cloth_line_name,product_code,product_name,startDate,endDate) {
    try {
        // 使用模板字符串或参数绑定来构造SQL语句（取决于你的后端API如何处理参数）
        const sqlStatement = "SELECT ls.t_dyelot_code as dyelot_code,\n" +
            "                          ls.t_product_code as product_code,\n" +
            "                          ls.t_product_name as product_name,\n" +
            "                          ls.t_degree_of_staining_code as degree_of_staining_code,\n" +
            "                          ls.t_degree_of_staining_name as degree_of_staining_name,\n" +
            "                          ls.t_label_color_name as label_color_name,\n" +
            "                          ls.t_fabric_sum as fabric_sum,\n" +
            "                          ls.t_fabric_label_weight_sum as fabric_label_weight_sum,\n" +
            "                          ls.t_pi_sum as pi_sum,\n" +
            "                          ls.t_xima_sum as xima_sum,\n" +
            "                          ls.t_kongcha as kongcha,\n" +
            "                          ls.t_tube_weight as tube_weight,\n" +
            "                          ls.t_loss_rate as loss_rate,\n" +
            "                          ls.t_remarks as remarks,\n" +
            "                          ls.t_in_date as in_date,\n" +
            "                          ls.t_workmanship_pay_project_name as workmanshipPayProjects, \n" +
            "                          ls.t_label_print_gsm as label_print_gsm, \n" +
            "                          ls.t_ingredients_type as ingredients_type, \n" +
            "                          ls.t_reset_source as reset_source, \n" +
            "                          d.t_cloth_line_name as cloth_line_name\n" +
            "                       from t_io_doc_loss ls left join t_dyelot_info d on ls.t_dyelot_code = d.t_dyelot_code,\n " +
            "                            t_dyelot_production_process_info pf \n" +
            "                       where ls.t_dyelot_code = pf.dyelot_code \n" +
            "                       and (ls.t_loss_rate<100 or (ls.t_loss_rate=100 and ls.t_product_code='000#')) \n" +
            "                       and ls.t_in_date BETWEEN CONVERT(datetime, '"+startDate+" 00:00:00') and CONVERT(datetime, '"+endDate+" 23:59:59')\n" +
            "                       and ls.pei_type like '%"+pei_type+"%' and d.t_cloth_line_name like '%"+cloth_line_name+"%' and ls.t_product_name like '%"+product_name+"%'  and ls.t_product_code like '%"+product_code+"%' ";

        const sql = Base64.encode(sqlStatement);
        // 发送POST请求到后端API，并传递参数
        const response = await axios.post(
            "http://8.134.79.23:34343/sql_actuator/execute",
            {
                "env": 'internet-ms-pe',
                "sql": sql,
            }
        );

        const responseData = response.data.data;
        return responseData;
    } catch (error) {
        console.error('Error fetching data:', error);
        throw error; // 抛出错误以便在组件中处理
    }
}

export async function expandRowsD(reset_source,pei_type,cloth_line_name,product_code,degree_of_staining_code,startDate,endDate) {

    try {
        let sqlWhere = " and ls.t_reset_source like '%"+reset_source+"%'  \n";
        if (reset_source=='/'){
            sqlWhere = "";
        }
        if (reset_source==null){
            sqlWhere = " and ls.t_reset_source is null \n";
        }
        // 使用模板字符串或参数绑定来构造SQL语句（取决于你的后端API如何处理参数）
        let sqlStatement = "SELECT ls.t_dyelot_code as dyelot_code,\n" +
            "              d.t_cloth_line_name as cloth_line_name,\n" +
            "              ls.t_product_code as product_code,\n" +
            "              ls.t_product_name as product_name,\n" +
            "              ls.t_degree_of_staining_code as degree_of_staining_code,\n" +
            "              ls.t_degree_of_staining_name as degree_of_staining_name,\n" +
            "              ls.t_label_color_name as label_color_name,\n" +
            "              ls.t_fabric_sum as fabric_sum,\n" +
            "              ls.t_fabric_label_weight_sum as fabric_label_weight_sum,\n" +
            "              ls.t_pi_sum as pi_sum,\n" +
            "              ls.t_xima_sum as xima_sum,\n" +
            "              ls.t_kongcha as kongcha,\n" +
            "              ls.t_tube_weight as tube_weight,\n" +
            "              ls.t_loss_rate as loss_rate,\n" +
            "              ls.t_remarks as remarks,\n" +
            "              FORMAT(ls.t_in_date, 'yyyy-MM-dd') as in_date, \n" +

            "              ls.t_workmanship_pay_project_name as workmanshipPayProjects, \n" +
            "              ls.t_label_print_gsm as label_print_gsm, \n" +
            "              ls.t_ingredients_type as ingredients_type, \n" +
            "              (case when ls.t_reset_source='' then '/' else ls.t_reset_source end) as reset_source \n" +

            "          from t_io_doc_loss ls left join t_dyelot_info d on ls.t_dyelot_code = d.t_dyelot_code, \n" +
            "               t_dyelot_production_process_info pf \n" +
            "           where ls.t_dyelot_code = pf.dyelot_code \n" +
            "           and ls.pei_type like '%"+pei_type+"%' and d.t_cloth_line_name like '%"+cloth_line_name+"%' \n" +sqlWhere+
            "           and ls.t_product_code = '"+product_code+"' and ls.t_degree_of_staining_code = '"+degree_of_staining_code+"' \n" +
            "           and ls.t_in_date BETWEEN CONVERT(datetime, '"+startDate+" 00:00:00') and CONVERT(datetime, '"+endDate+" 23:59:59')  \n"
        ;
        const sql = Base64.encode(sqlStatement);
        // 发送POST请求到后端API，并传递参数
        const response = await axios.post(
            "http://8.134.79.23:34343/sql_actuator/execute",
            {
                "env": 'internet-ms-pe',
                "sql": sql,
            }
        );

        const responseData = response.data.data;
        return responseData;
    } catch (error) {
        console.error('Error fetching data:', error);
        throw error; // 抛出错误以便在组件中处理
    }
}
